/*
	新闻列表
		新闻列表
		新闻详情
		新闻添加
*/
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var dictData = Cookies.getJSON('kplus_web_dict');
// dom
$(function(){
	layui.use(['layer', 'form', 'table'], function(){
		var layer = layui.layer;
		var form = layui.form;
		var table = layui.table;
		//base ajax data
		var ajaxData = {
			aId: adminLoginedInfo.aId,
			aToken: adminLoginedInfo.aToken
		};

		//获取新闻列表
		function newsList(ajaxData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
			$.ajax({
				url: BASE_SERVICEURL + URlS.news,
				type: 'GET', 
				data: ajaxData,
				success: function(responseData){
					if(responseData.msg.code == 200){
						layer.close(loadIndex);
						//console.log(responseData);
						createTable(createTableData(responseData.data))
					}else{
						layer.close(loadIndex);
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},
				error: function(responseData){
					layer.msg(MESSAGES.service_error, {time:2000, end: function(){
						location.href = './500.html';
					}})
				}
			})
		};
		newsList(ajaxData);

		//create table data
		function createTableData(data){
			var table_data = []
			for (var i = data.length - 1; i >= 0; i--) {
				var new_data = {
					newsId: data[i].w_news,
					title: data[i].news_title['cn'],
					new_tags: data[i].new_tags.join(';'),
					admin: dictData.admin[data[i].create_admin],
					create_time: formatTime(data[i].format_time),
					publish_time: data[i].publish_time,
					news_status: data[i].news_status
				};
				table_data.push(new_data);			
			};
			return table_data;
		}
		//table 构建
		function createTable(tableData){
			table.render({
				elem: '#tableContent',
				limit: 30, //分页
				height: 680,
				page: true,
				cols:[[ //表头
					{field: 'newsId', title: '新闻ID', sort: true, fixed: 'left', width:120},
					{field: 'title', title: '标题', sort: true},
					{field: 'new_tags', title: '摘要', sort: true} ,
					{field: 'admin', title: '创建者', width:110},
					{field: 'create_time', title: '创建时间'},
					{field: 'publish_time', title: '发布时间', sort: true},
					{field: 'news_status', title: '状态', width:80,sort: true},
					{field: 'option', title: '操作', toolbar:'#toolbar'}
				]],
				data:tableData
			});
		};
		//toolbar 查看详情
		table.on('tool(news-list-table)', function(obj){
			
			var layEvent = obj.event;
			var data = obj.data;
			if(layEvent == 'seeDetail'){
				//console.log(obj)
				x_admin_show('查看详情', 'news-detail.html?w_news='+data.newsId+'', 1200, 800);
			};
		})


	})
})











